package com.woniu.atms.controller;

import com.woniu.atms.exception.aft.car.CarIsNullException;
import com.woniu.atms.exception.aft.car.CarSeatIsAddException;
import com.woniu.atms.exception.pre.ticket.*;
import com.woniu.atms.util.Code;
import com.woniu.atms.util.ResponseResult;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 张奇
 * @title: G5ExceptionHandler
 * @projectName g5_atms
 * @description: TODO
 * @date 2022/1/18 9:58
 */
@RestControllerAdvice
public class G5ExceptionHandler {

    /**
     * 捕获所有参数格式错误异常
     * @param ex 捕获的异常类
     * @return 返回错误码信息并发回浏览器
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseResult argsCheckedFail(MethodArgumentNotValidException ex){
        Map<String,String> msgs = new HashMap<>();
        ex.getFieldErrors().forEach(e -> msgs.put(e.getField(), e.getDefaultMessage()));
        return new ResponseResult(Code.FROM_ERROR,"参数预检查错误",msgs);
    }

    /**
     * JSON格式转换失败异常1
     * @param ex 异常的类
     * @return 返回错误码到浏览器
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResponseResult jsonParseError(HttpMessageNotReadableException ex){
        return new ResponseResult(Code.FROM_ERROR, "请求格式错误");
    }

    /**
     * JSON格式转换失败异常2
     * @param ex 异常的类
     * @return 返回错误码到浏览器
     */
    @ExceptionHandler(HttpMessageConversionException.class)
    public ResponseResult jsonConversionError(HttpMessageConversionException ex){
        return new ResponseResult(Code.FROM_ERROR, "格式转换失败");
    }

    /**
     * 查询不到车辆异常
     * @param ex
     * @return
     */
    @ExceptionHandler(CarIsNullException.class)
    public ResponseResult carIsNullExceptionError(CarIsNullException ex){
        return new ResponseResult(Code.FROM_ERROR, ex.getMessage());
    }

    /**
     * 车辆已添加座位异常
     * @param ex
     * @return
     */
    @ExceptionHandler(CarSeatIsAddException.class)
    public ResponseResult carSeatIsAddExceptionError(CarSeatIsAddException ex){
        return new ResponseResult(Code.FROM_ERROR, ex.getMessage());
    }


    /**
     * 查询不到车票异常
     * @param ex
     * @return
     */
    @ExceptionHandler(TicketIsNullException.class)
    public ResponseResult ticketIsNullExceptionError(TicketIsNullException ex){
        return new ResponseResult(Code.FROM_ERROR, ex.getMessage());
    }

    /**
     * 车票已经被退票
     * @param ex
     * @return
     */
    @ExceptionHandler(TicketIsReException.class)
    public ResponseResult ticketIsReExceptionError(TicketIsReException ex){
        return new ResponseResult(Code.FROM_ERROR, ex.getMessage());
    }

    /**
     * 车票已经被使用
     * @param ex
     * @return
     */
    @ExceptionHandler(TicketIsUseException.class)
    public ResponseResult ticketIsUseExceptionError(TicketIsUseException ex){
        return new ResponseResult(Code.FROM_ERROR, ex.getMessage());
    }

    /**
     * 车票还未被使用
     * @param ex
     * @return
     */
    @ExceptionHandler(TicketIsUnUseException.class)
    public ResponseResult ticketIsUnUseExceptionError(TicketIsUnUseException ex){
        return new ResponseResult(Code.FROM_ERROR, ex.getMessage());
    }

    /**
     * 车票超过退票时间
     * @param ex
     * @return
     */
    @ExceptionHandler(TicketReOutTimeException.class)
    public ResponseResult ticketReOutTimeExceptionError(TicketReOutTimeException ex){
        return new ResponseResult(Code.FROM_ERROR, ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ResponseResult<Void> handlerException(Exception exception) {
        exception.printStackTrace();
        return ResponseResult.ERROR;
    }


}
